package com.woniuxy.three.pms.order.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.three.pms.bean.OrderBean;
import com.woniuxy.three.pms.bean.UserBean;
import com.woniuxy.three.pms.bean.VipBean;
import com.woniuxy.three.pms.dao.UserBeanMapper;
import com.woniuxy.three.pms.dao.VipBeanMapper;

@Service
public class UpgradeService {

	@Autowired
	private VipBeanMapper vipDao;
	@Autowired
	private UserBeanMapper userDao;
	
	public boolean upgradeUser(OrderBean order) {
		BigDecimal totalPrice = order.getTotalPrice();
		Integer uid = order.getUserId().getId();
		UserBean user = userDao.selectByPrimaryKey(uid);
		BigDecimal cumulative = user.getCumulative();
		cumulative.add(totalPrice);
		user.setCumulative(cumulative);
		List<VipBean> list = vipDao.findAllVip();
		double subMax = 0;
		for (VipBean vipBean : list) {
			BigDecimal quota = new BigDecimal(vipBean.getQuota());
			if(cumulative.subtract(quota).doubleValue()>subMax) {
				subMax=cumulative.subtract(quota).doubleValue();
				user.setVipBean(vipBean);
				user.setVipId(vipBean.getId());
			}
		}
		int number = userDao.updateByPrimaryKeySelective(user);
		if(number>0) {
			return true;
		}
		return false;
	}
	
}
